package org.zhenglide.oa.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.zhenglide.oa.service.IDeployService;
import org.zhenglide.oa.vo.WorkflowBean;

/**
 * 
 * 流程管理
 * 
 * @author 84441
 *
 */
@Controller
@RequestMapping("/process")
public class ProcessController {
	@Autowired
	IDeployService deployService;

	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		List<Deployment> depList = deployService.getDeploymentList();
		List<ProcessDefinition> pdList = deployService.getProcessDefineList();

		req.setAttribute("depList", depList);
		req.setAttribute("pdList", pdList);
		return "workflow/workflow";
	}
	@RequestMapping("delDeploy")
	public String delDeploy(@RequestParam("deploymentId") String deploymentId, HttpServletRequest req) {
		deployService.deleteProcessDefinitionByDeploymentId(deploymentId);
		return "redirect:/process/list";
	}
	@RequestMapping("newdeploy")
	public String newdeploy(@RequestParam("file") MultipartFile  multipartFile, String filename, HttpServletRequest req) {
		try {
			InputStream inputStream = multipartFile.getInputStream();
			File file = new File(multipartFile.getOriginalFilename());
			inputStreamToFile(inputStream,file);
			deployService.deployZip(file, filename);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		 return "redirect:/process/list";
	}

	private void inputStreamToFile(InputStream ins, File file) {

		try {
			OutputStream os = new FileOutputStream(file);
			int bytesRead = 0;
			byte[] buffer = new byte[8192];

			while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			os.close();
			ins.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/imageView")
	public String imageView(HttpServletResponse resp, WorkflowBean workflowBean) {

		// 1：获取页面传递的部署对象ID和资源图片名称
		// 部署对象ID
		String deploymentId = workflowBean.getDeploymentId();
		// 资源图片名称
		String imageName = workflowBean.getImageName();
		// 2：获取资源文件表（act_ge_bytearray）中资源图片输入流InputStream
		InputStream in = deployService.findImageInputStream(deploymentId, imageName);
		// 3：从response对象获取输出流
		try {
			OutputStream out = resp.getOutputStream();
			// 4：将输入流中的数据读取出来，写到输出流中
			for (int b = -1; (b = in.read()) != -1;) {
				out.write(b);
			}
			out.close();
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 将图写到页面上，用输出流写
		return null;

	}
}
